//
//  ViewController.m
//  06-超级猜图
//
//  Created by wangneihan on 15/12/14.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "ViewController.h"
#import "Questions.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *pageIndex;
@property (weak, nonatomic) IBOutlet UIButton *conBtn;
@property (weak, nonatomic) IBOutlet UILabel *questionTitle;
@property (weak, nonatomic) IBOutlet UIButton *centerImage;
@property (weak, nonatomic) IBOutlet UIButton *tipBtn;

@property (weak, nonatomic) IBOutlet UIButton *bigPicBtn;
@property (weak, nonatomic) IBOutlet UIButton *helpBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UIView *answersView;
@property (weak, nonatomic) IBOutlet UIView *optionsView;

//index
@property (nonatomic,assign) int qIndex;
//cons
@property (nonatomic,assign) int cons;
//题目
@property (nonatomic,strong) NSArray *questions;

-(NSArray *)questions;

-(IBAction)nextQuestion;

-(void)loadAnswerView:(Questions *)question;

-(IBAction)answerClick:(UIButton *)sender;

-(void)loadOptionsView:(Questions *)question;

-(IBAction)optionClick:(UIButton *)sender;

@end

@implementation ViewController

-(NSArray *)questions{
    if(_questions == nil)
    {
        _questions = [Questions loadQuestions];
    }
    return _questions;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.qIndex = 0;
    [self loadQuestion:self.qIndex];
    
}

-(void)loadQuestion:(int)index
{
    Questions *question = self.questions[index];
    
    self.pageIndex.text = [NSString stringWithFormat:@"%d/%d",index+1 , self.questions.count];
    self.questionTitle.text = [NSString stringWithFormat:@"%@" ,question.title];
    
    [self.centerImage setImage:[UIImage imageNamed:question.icon] forState:UIControlStateNormal];
    
    [self loadAnswerView:question];
    [self loadOptionsView:question];
}

-(IBAction)nextQuestion{
    [self.answersView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.qIndex += 1;
    [self loadQuestion:self.qIndex];
}

-(void)loadAnswerView:(Questions *)question
{
    int length = question.answer.length;
    
    CGFloat margin = 20;
    
    CGFloat btnSize = 35;
    
    CGFloat widthCount = btnSize*length + margin*(length-1);
    
    CGFloat firstMargin = (self.view.frame.size.width - widthCount)/2;
    
    for (int x = 0; x < length; x++) {
        
        CGFloat marginX = firstMargin + margin*x + btnSize*x;
        
        UIButton *ansBtn = [[UIButton alloc]initWithFrame:CGRectMake(marginX, 0, btnSize, btnSize)];
        
        [self.answersView addSubview:ansBtn];
        
        [ansBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [ansBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
        [ansBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
        
        [ansBtn addTarget:self action:@selector(answerClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

-(IBAction)answerClick:(UIButton *)sender{
    if(sender.currentTitle == nil) return;
    [[self.optionsView viewWithTag:sender.tag] setHidden:NO];
    [sender setTitle:nil forState:UIControlStateNormal];
}

-(void)loadOptionsView:(Questions *)question
{
    int colume = 0;
    
    CGFloat btnSize = 35;
    
    CGFloat margin = 20;
    
    for (int x = 0; x < 10 ; x++) {
        CGFloat widthCount = btnSize * x + margin * (x-1);
        if(widthCount < self.view.frame.size.width)
        {
            colume = x;
        }
    }
    
    CGFloat btnWidth = btnSize*colume + (colume - 1)*margin;
    
    CGFloat firstMargin = (self.view.frame.size.width - btnWidth)/2;
    
    for (int tempTag = 0; tempTag < question.options.count ; tempTag++) {
        
        int row = tempTag/colume;
        
        int tempColumn = tempTag%colume;
        
        CGFloat marginX = firstMargin + tempColumn * (margin + btnSize);

        CGFloat marginY = (margin + btnSize)*row;
        
        UIButton *optionsBtn = [[UIButton alloc]initWithFrame:CGRectMake(marginX, marginY, btnSize, btnSize)];
        
        [self.optionsView addSubview:optionsBtn];
        
        optionsBtn.tag = tempTag;
        
        [optionsBtn setTitle:question.options[tempTag] forState:UIControlStateNormal];
        [optionsBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [optionsBtn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
        [optionsBtn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
        
        [optionsBtn addTarget:self action:@selector(optionClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(IBAction)optionClick:(UIButton *)sender{
    
    sender.hidden = YES;
    
    for (UIButton *tempBtn in self.answersView.subviews) {
        
        if(tempBtn.currentTitle == nil)
        {
            [tempBtn setTitle:sender.currentTitle forState:UIControlStateNormal];
            tempBtn.tag = sender.tag;
            break;
        }
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
